"""
Not a good modification.
"""

import os
import sys
import logging
import logging.handlers
import platform
import warnings

handler = None
visited_loggers = set()

DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs.tmp.d')
os.makedirs(DIR, exist_ok=True)


def build_logger(logger_name, logger_filename, logger_level=logging.DEBUG, logdir=DIR, when_rotate='H'):
    global handler

    formatter = logging.Formatter(
        fmt="%(asctime)s | %(levelname)s | %(name)s | %(message)s | %(pathname)s[line:%(lineno)d]",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

    # Set the format of root handlers
    # if not logging.getLogger().handlers:
    #     if sys.version_info[1] >= 9:
    #         # This is for windows
    #         logging.basicConfig(level=logger_level, encoding="utf-8")
    #     else:
    #         if platform.system() == "Windows":
    #             warnings.warn(
    #                 "If you are running on Windows, "
    #                 "we recommend you use Python >= 3.9 for UTF-8 encoding."
    #             )
    #         logging.basicConfig(level=logger_level)
    # logging.getLogger().handlers[0].setFormatter(formatter)

    # Redirect stdout and stderr to loggers
    stdout_logger = logging.getLogger("stdout")
    stdout_logger.setLevel(logger_level)
    sl = StreamToLogger(stdout_logger, logging.INFO, terminal=sys.stdout)
    sys.stdout = sl

    stderr_logger = logging.getLogger("stderr")
    stderr_logger.setLevel(logging.ERROR)
    sl = StreamToLogger(stderr_logger, logging.ERROR, terminal=sys.stderr)
    sys.stderr = sl

    # Get logger
    logger = logging.getLogger(logger_name)
    logger.setLevel(logger_level)
    if logger not in visited_loggers:
        handler = logging.StreamHandler()
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # if logdir is empty, then don't try output log to local file
    if logdir != "":
        os.makedirs(logdir, exist_ok=True)
        filename = os.path.join(logdir, logger_filename)
        handler = logging.handlers.TimedRotatingFileHandler(
            filename, when=when_rotate, utc=True, encoding="utf-8"
        )
        handler.setFormatter(formatter)

        for l in [stdout_logger, stderr_logger, logger]:
            if l in visited_loggers:
                continue
            visited_loggers.add(l)
            l.addHandler(handler)
            # l.addHandler(logging.StreamHandler())

    return logger


class StreamToLogger(object):
    """
    Fake file-like stream object that redirects writes to a logger instance.
    """

    def __init__(self, logger, log_level=logging.INFO, terminal=sys.stdout):
        # self.terminal = sys.stdout
        self.terminal = terminal
        self.logger = logger
        self.log_level = log_level
        self.linebuf = ""

    def __getattr__(self, attr):
        return getattr(self.terminal, attr)

    def write(self, buf):
        temp_linebuf = self.linebuf + buf
        self.linebuf = ""
        for line in temp_linebuf.splitlines(True):
            # From the io.TextIOWrapper docs:
            #   On output, if newline is None, any '\n' characters written
            #   are translated to the system default line separator.
            # By default sys.stdout.write() expects '\n' newlines and then
            # translates them so this is still cross platform.
            if line[-1] == "\n":
                encoded_message = line.encode("utf-8", "ignore").decode("utf-8")
                self.logger.log(self.log_level, encoded_message.rstrip())
                print(line, file=self.terminal, flush=True, end='')
            else:
                self.linebuf += line

    def flush(self):
        if self.linebuf != "":
            encoded_message = self.linebuf.encode("utf-8", "ignore").decode("utf-8")
            self.logger.log(self.log_level, encoded_message.rstrip())
            print(self.linebuf, file=self.terminal, flush=True, end='')
        self.linebuf = ""